package com.jjiang.bbmall.search.thread;

import java.util.concurrent.*;

public class ThreadTest {
    // 4. 线程池【在业务代码里面用这个，当前项目有几个就可以】
    public static ExecutorService executorService = Executors.newFixedThreadPool(10);


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main...start...");

//        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程Id：" + Thread.currentThread().getId());
//            long result = Thread.currentThread().getId() * 2;
//            System.out.println("运行结果：" + result);
//        }, executorService);

//        CompletableFuture<Long> completableFuture = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程Id：" + Thread.currentThread().getId());
//            long result = Thread.currentThread().getId() * 2;
//            System.out.println("运行结果：" + result);
//            return result;
//        }, executorService);
//        Long futureResult = null;
//        try {
//            futureResult = completableFuture.get();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }
//        System.out.println("result=" + futureResult);

//        CompletableFuture<Long> completableFuture = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程Id：" + Thread.currentThread().getId());
//            long result = Thread.currentThread().getId() / 0;
//            System.out.println("运行结果：" + result);
//            return result;
//        }, executorService).whenComplete((result, exception) -> {
//            // 虽然可以得到异常信息，但是无法修改结果
//            System.out.println("异步任务完成了...,result=" + result + ",exception=" + exception);
//        }).exceptionally((throwable -> {
//            // 可以指定出现异常的默认返回
//            return 100L;
//        }));
//        Long futureResult = null;
//        try {
//            futureResult = completableFuture.get();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }
//        System.out.println("result=" + futureResult);

//        CompletableFuture<Long> completableFuture = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程Id：" + Thread.currentThread().getId());
//            long result = Thread.currentThread().getId() * 2;
//            System.out.println("运行结果：" + result);
//            return result;
//        }, executorService).handle((result, exception)->{
//            if (result!=null){
//                return result * 10;
//            }else {
//                return 100L;
//            }
//        });
//        Long futureResult = null;
//        try {
//            futureResult = completableFuture.get();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }
//        System.out.println("result=" + futureResult);

//        // 不能获取上一步的执行结果
//        CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程Id：" + Thread.currentThread().getId());
//            long result = Thread.currentThread().getId() * 2;
//            System.out.println("运行结果：" + result);
//            return result;
//        }, executorService).thenRunAsync(()->{
//            System.out.println("任务2完成了");
//        });

//        // 能获取上一步的执行结果，但是无法返回结果
//        CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程Id：" + Thread.currentThread().getId());
//            long result = Thread.currentThread().getId() * 2;
//            System.out.println("运行结果：" + result);
//            return result;
//        }, executorService).thenAcceptAsync((result) -> {
//            System.out.println("任务2完成了,result=" + result);
//        });

//        // 能获取上一步的执行结果，并且还可以返回结果
//        CompletableFuture<Long> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程Id：" + Thread.currentThread().getId());
//            long result = Thread.currentThread().getId() * 2;
//            System.out.println("运行结果：" + result);
//            return result;
//        }, executorService).thenApplyAsync((result) -> {
//            System.out.println("任务2完成了,result=" + result);
//            return result * 10;
//        });
//        Long futureResult = null;
//        try {
//            futureResult = future.get();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }
//        System.out.println("result=" + futureResult);

        // 能获取上一步的执行结果，并且还可以返回结果
        CompletableFuture<Long> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1，当前线程Id：" + Thread.currentThread().getId());
            long result = Thread.currentThread().getId() * 2;
            System.out.println("运行结果：" + result);
            return result;
        }, executorService);
        CompletableFuture<Long> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2，当前线程Id：" + Thread.currentThread().getId());
            try {
                Thread.sleep(2000);
                System.out.println("运行结果：666L");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 666L;
        }, executorService);

        CompletableFuture<Object> future = CompletableFuture.anyOf(future1, future2);

//        future.get();

        Long result = (Long) future.get();
        System.out.println("result=" + result);

        System.out.println("main...end...");
    }

//    public static void main(String[] args) {
//        System.out.println("main...start...");
//        /*
//        初始化线程的4种方式
//           1. 继承Thread
//           2. 实现Runnable接口
//           3. 实现callable接口 + FutureTask（可以拿到返回结果，可以处理异常）
//           4. 线程池
//         */
//        // 1. 继承Thread
//        Thread01 thread01 = new Thread01();
//        thread01.start();
//
//        // 2. 实现Runnable接口
//        Runnable01 runnable01 = new Runnable01();
//        new Thread(runnable01).start();
//
//        // 3. 实现callable接口 + FutureTask（可以拿到返回结果，可以处理异常）
//        FutureTask<Long> longFutureTask = new FutureTask<>(new Callable01());
//        new Thread(longFutureTask).start();
//        Long futureTaskResult = null;
//        try {
//            futureTaskResult = longFutureTask.get(); //阻塞等待结果
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }
//        System.out.println("futureTaskResult=" + futureTaskResult);
//
//        // 4. 线程池【在业务代码里面用这个，当前项目有几个就可以】
//        executorService.execute(new Runnable01());
//
//        System.out.println("main...end...");
//    }

    public static class Thread01 extends Thread {
        @Override
        public void run() {
            System.out.println("当前线程Id：" + Thread.currentThread().getId());
            long result = Thread.currentThread().getId() * 2;
            System.out.println("运行结果：" + result);
        }
    }

    public static class Runnable01 implements Runnable {
        @Override
        public void run() {
            System.out.println("当前线程Id：" + Thread.currentThread().getId());
            long result = Thread.currentThread().getId() * 2;
            System.out.println("运行结果：" + result);
        }
    }

    public static class Callable01 implements Callable<Long> {
        @Override
        public Long call() throws Exception {
            System.out.println("当前线程Id：" + Thread.currentThread().getId());
            long result = Thread.currentThread().getId() * 2;
            System.out.println("运行结果：" + result);
            return result;
        }
    }
}
